﻿namespace DesignModeConsole.行为型.解释器模式.自定义1;

/// <summary>
/// and 表达式
/// </summary>
public class AndExpression3 : IAlertRuleExpression3
{
    private readonly List<IAlertRuleExpression3> _expressions = new List<IAlertRuleExpression3>();

    public AndExpression3(string strAndExpression)
    {
        var strExpressions = strAndExpression.Split("and");
        foreach (var e in strExpressions)
        {
            _expressions.Add(new RegexExpression3(e));
        }
    }

    public AndExpression3(IEnumerable<string> param)
    {
        var strExpressions = string.Join(" ", param).Split("and");
        foreach (var e in strExpressions)
        {
            _expressions.Add(new RegexExpression3(e));
        }
    }

    public AndExpression3(IEnumerable<IAlertRuleExpression3> expressions)
    {
        _expressions.AddRange(expressions);
    }

    public bool Interpret(string tableName, string columnName)
    {
        return _expressions.All(item => item.Interpret(tableName, columnName));
    }
}

/// <summary>
/// or 表达式
/// </summary>
public class OrExpression3 : IAlertRuleExpression3
{
    private readonly List<IAlertRuleExpression3> _expressions = new List<IAlertRuleExpression3>();

    public OrExpression3(string strOrExpression)
    {
        var andExpressions = strOrExpression.Split("or");
        foreach (var andExpr in andExpressions)
        {
            _expressions.Add(new AndExpression3(andExpr));
        }
    }

    public OrExpression3(IEnumerable<IAlertRuleExpression3> expressions)
    {
        _expressions.AddRange(expressions);
    }

    public bool Interpret(string tableName, string columnName)
    {
        return _expressions.Any(item => item.Interpret(tableName, columnName));
    }
}